package com.yy.permissions_general.common.config.mybatis;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yy.permissions_general.utils.SecurityUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName MyMetaObjectHandler
 * @Description: TODO  mybatis-plus配置添加时间和更新时间
 * @Author YangYI
 * @Date 2021/9/27
 * @Version V1.0
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //属性名称，不是字段名称
        this.setFieldValByName("createTime", new Date(), metaObject);
        if (ObjectUtil.isNotNull(SecurityUtil.getCurrentUser())){
            this.setFieldValByName("createId", SecurityUtil.getCurrentUser().getId(), metaObject);
            this.setFieldValByName("createName", SecurityUtil.getCurrentUser().getUsername(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        if (ObjectUtil.isNotNull(SecurityUtil.getCurrentUser())) {
            this.setFieldValByName("updateId", SecurityUtil.getCurrentUser().getId(), metaObject);
            this.setFieldValByName("updateName", SecurityUtil.getCurrentUser().getUsername(), metaObject);
        }
    }
}
